'use strict'
var OrganizationModel = require('../../model/organization');
var Data = require('../../util/data')

class Organization {
    async getAll(req, res, next) {
        try {
            const organizations = await OrganizationModel.find({});
            res.send(organizations);
        } catch (err) {
            res.send(Data.throwError('获取组织数据失败'))
        }
    }
    async get(req, res, next) {
        try {
            const id = req.params.id
            const organizations = await OrganizationModel.findOne({ '_id': id });
            res.send(organizations);
        } catch (err) {
            res.send(Data.throwError('获取组织数据失败'))
        }
    }
    async post(req, res, next) {
        try {
            const item = req.body
            const organizations = await OrganizationModel.insertMany(item);
            res.send(organizations);
        } catch (err) {
            res.send(Data.throwError('创建组织数据失败'))
        }
    }
    async delete(req, res, next) {
        try {
            const id = req.params.id
            const organizations = await OrganizationModel.deleteOne({ _id: id });
            res.send(organizations);
        } catch (err) {
            res.send(Data.throwError('删除组织数据失败'))
        }
    }
    async put(req, res, next) {
        try {
            const id = req.params.id
            const data = req.body
            const organizations = await OrganizationModel.update({ _id: id }, data);
            res.send(organizations);
        } catch (err) {
            res.send(Data.throwError('修改组织数据失败'))
        }
    }
}

module.exports = new Organization()